<?Php

if(!defined('mySite')) {
    exit(1);
}

class TemplatePage extends Page {

    protected function pageName() {
        return sprintf(_('%s Template Page'), Util::config('site', 'name'));
    }

    protected function title() {
        return sprintf(_('%s ::: %s'), Util::config('site', 'name'), $this->pageName());
    }

    protected function base() {
        return APPLICATIONROOT;
    }

    protected function meta() {
        $meta = array();

        $meta[] = new Meta('author', Util::config('site', 'author'));

        return $meta;
    }

    protected function link() {
        $link = array();
        $link[] = new Link(array('href' => 'css/clear.css',
                    'rel' => 'stylesheet',
                    'type' => 'text/css',
                    'media' => 'all'));
        return $link;

    }

    protected function body() {
        $this->elementStart('div', 'wrapper');
        $this->topArea();
        $this->mainArea();
        $this->bottomArea();
        $this->elementEnd('div');
    }

    protected function topArea() {
        $this->elementStart('div', 'topArea');
        $this->logos();
        $this->topMenu();
        $this->elementEnd('div');
    }

    protected function logos() {
        $this->elementStart('div', 'logos');

        $this->elementStart('a', array('id' => 'logo_iq',
                            'href' => 'http://www.iqm.unicamp.br',
                            'title' => 'Instituto de Química'));
        $this->element('img', array('src' =>
                        'css/imagens/logo_iq.png',
                        'alt' => 'Instituto de Química'));
        $this->elementEnd('a');

        $this->elementStart('a', array('id' => 'logo_unicamp',
                    'href' => 'http://www.unicamp.br', 'title' => 'Unicamp'));
        $this->element('img', array('src' =>
                        'css/imagens/logo_unicamp.png', 'alt' => 'Unicamp'));
        $this->elementEnd('a');
        
        $this->elementEnd('div');
    }

    protected function topMenu() {
        $user = $this->currentUser();

        $this->elementStart('div', 'topMenu');

        $top_menu_left = new Menu('topMenuAcoes');
        $top_menu_left->addItem('topMenu_inicial', _('Página Inicial'),
                Util::localUrl('inicial'), _('Página Inicial'));

        $privilegio = $this->currentPrivilegio();
        if($privilegio <= 0) {
            $top_menu_left->addItem('topMenu_cadastro', _('Solicitação'),
                    Util::localUrl('cadastro'),
                    _('Inserir solicitação de experimento'));
            $top_menu_left->addItem('topMenu_remocao', _('Remover Solicitação'),
                    Util::localUrl('remocao'),
                    _('Remover solicitação de experimento'));
        }

        if($privilegio == 2) {
            $top_menu_left->addItem('topMenu_regocorrencia', _('Registrar Ocorrência'),
                    Util::localUrl('regocorrencia'),
                    _('Registrar uma ocorrência'));
            $top_menu_left->addItem('topMenu_relatorio', _('Relatório'),
                    Util::localUrl('relatorio'),
                    _('Relatório de solicitações'));
        }

        if($privilegio >= 1) {
            $top_menu_left->addItem('topMenu_ocorrencias', _('Ocorrências'),
                    Util::localUrl('ocorrencias'),
                    _('Relatório de ocorrências'));
        }

        $top_menu_left->flush($this, $this->topMenuCurrent());

        $top_menu_right = new Menu('topMenuUsuario');
        if(!$user) {
            $top_menu_right->addItem('topMenu_login', _('Login'),
                    Util::localUrl('login'), _('Realizar login'));
        }
        else {
            $top_menu_right->addItem('topMenu_sair', _('Sair'),
                    Util::localUrl('sair'), _('Sair do sistema'));
        }
        $top_menu_right->flush($this, $this->topMenuCurrent());

        $this->elementEnd('div');
    }

    protected function topMenuCurrent() {
    }

    protected function mainArea() {
        $this->elementStart('div', 'mainArea');
        $this->leftArea();
        $this->contentArea();
        $this->elementEnd('div');
    }

    protected function leftArea() {
        $this->elementStart('div', 'leftArea');

        $this->elementEnd('div');
    }

    protected function contentArea() {
        $this->elementStart('div', 'contentArea');
        $this->content();
        $this->elementEnd('div');
    }

    protected function content() {
    }

    protected function bottomArea() {
        $this->elementStart('div', 'bottomArea');
        //$this->footer();
        $this->elementEnd('div');
    }

    protected function footer() {
        $this->elementStart('div', 'footer');

        $this->elementStart('span', 'footerCopyright');
        $this->raw('&#169;');
        $this->text('Copyright 1997-' . date('Y'));
        $this->elementEnd('span');

        $this->element('span', 'footerOrganization',
                Util::config('site', 'organization'));

        $this->elementStart('span', 'footerContact');
        $this->element('a',
                array('href' => 'mailto:'.Util::config('site', 'email')),
                Util::config('site', 'email'));
        $this->elementEnd('span');

        $this->elementEnd('div');
    }

    protected function currentUser() {
        if( isset($_COOKIE[Util::config('site', 'sessionname')]) ||
                ($this->isRegisteredSession('logged') === true) ) {
            $this->startSession(Util::config('site', 'sessionname'));
            return $this->isRegisteredSession('usuario');
        }
        return false;
    }

    protected function currentPrivilegio() {
        if($user = $this->currentUser()) {
            return $user['privilegio'];
        }
        return -1;
    }

    protected function pageError($msg, $acoes = null) {
        $err = new ErroPage($msg, $acoes);
        $err->constructPage();
    }

    protected function boxMessage($msg, $tipo = 'erro') {
        if(!empty($msg)) {
            $this->elementStart('div', array('class' => 'msg_'.$tipo));
            if(is_string($msg)) {
                $this->element('p', null, $msg);
            }
            else if(is_array($msg)) {
                foreach($msg as $linha) {
                    $this->element('p', null, $linha);
                }
            }
            else {
                $this->element('p', null,
                        'Erro desconhecido! Por favor contate o administrador');
            }
            $this->elementEnd('div');
        }
    }

}
